package com.ymdd.library.pickerview.view;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.ymdd.library.R;
import com.ymdd.library.pickerview.adapter.WeekAdapter;
import com.ymdd.library.pickerview.adapter.WeekNumericWheelAdapter;
import com.ymdd.library.pickerview.lib.WheelView;
import com.ymdd.library.pickerview.listener.OnItemSelectedListener;
import com.ymdd.library.pickerview.utils.PickerConfig;
import com.ymdd.library.pickerview.utils.PickerContants;
import com.ymdd.library.pickerview.utils.TimeRepository;
import com.ymdd.library.pickerview.utils.WheelCalendar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Logger;

/**
 * Created by gyy on 2018/1/23.
 */

public class MonthAndDayWheel {
    Context mContext;
    WheelView day, hour, minute;
    WeekNumericWheelAdapter mDayAdapter, mHourAdapter, mMinuteAdapter;

    PickerConfig mPickerConfig;
    TimeRepository mRepository;

    OnItemSelectedListener dayListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(int index) {
            Log.e("onItemSelected",  "dayListener");
            updateHours();
            updateMinutes();
        }
    };
    OnItemSelectedListener minuteListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(int index) {
            Log.e("onItemSelected",  "minuteListener");
            updateMinutes();
        }
    };

    public MonthAndDayWheel(View view, PickerConfig pickerConfig) {
        mPickerConfig = pickerConfig;

        mRepository = new TimeRepository(pickerConfig);
        mContext = view.getContext();
        initialize(view);
    }

    public void initialize(View view) {
        initView(view);
        initDay();
        initHour();
        initMinute();
    }


    void initView(View view) {
        day = (WheelView) view.findViewById(R.id.day);
        hour = (WheelView) view.findViewById(R.id.hour);
        minute = (WheelView) view.findViewById(R.id.min);

        day.setOnItemSelectedListener(dayListener);
        hour.setOnItemSelectedListener(minuteListener);
    }

    void initDay() {
        updateDays2();

        int minDay = mRepository.getMinDay();
        WheelCalendar defaultCalendar = mRepository.getDefaultCalendar();
        day.setCurrentItem(defaultCalendar.day - minDay);
        day.setCyclic(mPickerConfig.cyclic);
    }

    void initHour() {
        updateHours();

        int minHour = mRepository.getMinHour(getCurrentDay());
        hour.setCurrentItem(mRepository.getDefaultCalendar().hour - minHour);
        hour.setCyclic(mPickerConfig.cyclic);
    }

    void initMinute() {
        updateMinutes();
        int minMinute = mRepository.getMinMinute(getCurrentDay(), getCurrentHour2());

        minute.setCurrentItem(mRepository.getDefaultCalendar().minute - minMinute);
        minute.setCyclic(mPickerConfig.cyclic);

    }

    void updateDays2() {
        if (day.getVisibility() == View.GONE)
            return;

        mDayAdapter = new WeekAdapter(mContext, 0, mPickerConfig.addDay, null, mPickerConfig.mDay , mPickerConfig);
        mDayAdapter.setConfig(mPickerConfig);
        day.setAdapter(mDayAdapter);

        int dayCount = mDayAdapter.getItemsCount();
        if (day.getCurrentItem() >= dayCount) {
            day.setCurrentItem(dayCount - 1);
        }
    }

    void updateHours() {
        if (hour.getVisibility() == View.GONE)
            return;

        int curDay = getCurrentDay();
        int minHour = mRepository.getMinHour(curDay);
        int maxHour = mRepository.getMaxHour(curDay);

        mHourAdapter = new WeekNumericWheelAdapter(mContext, minHour, maxHour, PickerContants.FORMAT, mPickerConfig.mHour);
        mHourAdapter.setConfig(mPickerConfig);
        hour.setAdapter(mHourAdapter);

        if (curDay == 0)
            hour.setCurrentItem(0);
    }

    void updateMinutes() {
        if (minute.getVisibility() == View.GONE)
            return;

        int minMinute = mRepository.getMinMinute(getCurrentDay(), getCurrentHour2());
        int maxMinute = mRepository.getMaxMinute(getCurrentDay());

        mMinuteAdapter = new WeekNumericWheelAdapter(mContext, minMinute, maxMinute, PickerContants.FORMAT, mPickerConfig.mMinute);
        mMinuteAdapter.setConfig(mPickerConfig);
        minute.setAdapter(mMinuteAdapter);

        if (getCurrentDay() == 0)
            minute.setCurrentItem(0);
    }


    public int getCurrentDay() {
        return day.getCurrentItem();
    }

    public String getCurrentDayString() {
        int currentDay = getCurrentDay();

        Date date = mDayAdapter.times.get(currentDay);
        String DATE_FORMAT = "yyyy-MM-dd";
        String mResult = null;
        if (date != null) {
            DateFormat mDateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
            mResult = mDateFormat.format(date);
        }


        return mResult+" "+getCurrentHour()+":"+getCurrentMinute();
    }


    public int getCurrentHour() {
        return hour.getCurrentItem() + mRepository.getMinHour(getCurrentDay());
    }

    public int getCurrentHour2() {
        return hour.getCurrentItem();
    }

    public int getCurrentMinute() {
        return minute.getCurrentItem() + mRepository.getMinMinute(getCurrentDay(), getCurrentHour2());
    }

    /**
     * 设置是否循环滚动
     */
    public void setCyclic(boolean cyclic) {
        day.setCyclic(cyclic);
        hour.setCyclic(cyclic);
        minute.setCyclic(cyclic);
    }


}

